package com.android.example.myapp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Map;


public class MainActivity extends AppCompatActivity {
    private String userName = "abc";//初始化账户
    private String pass = "123";//初始化密码

    private TextView tv_share;
    private static final String TAG = "tag";
    private Button btnLogin;
    private EditText edAccount,etPassword;
    private TextView abcd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin = findViewById(R.id.btn_login);//登入按钮
        edAccount = findViewById(R.id.et_account);
        etPassword= findViewById(R.id.et_password);
        abcd = findViewById(R.id.cb_agree);//还没有账户
        tv_share = findViewById(R.id.tv_share);
        readSharedPreferences();//获取参数

        //登入设置
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = edAccount.getText().toString();
                String password = etPassword.getText().toString();

                Log.d(TAG,"onClick------------" + account);
                Log.d(TAG,"password------------" + password);

                if(TextUtils.equals(account,userName)){
                    if(TextUtils.equals(password,pass)){
                        Toast.makeText(MainActivity.this,"恭喜你，登入成功！",Toast.LENGTH_LONG).show();
                    }else {
                        Toast.makeText(MainActivity.this,"密码错误！",Toast.LENGTH_LONG).show();
                    }
                }else {
                    Toast.makeText(MainActivity.this,"用户名错误！",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    //获取共享参数
    private void readSharedPreferences() {
        // 从share.xml中获取共享参数对象
        SharedPreferences shared = getSharedPreferences("share", MODE_PRIVATE);
        //String desc = "这是信息：";//观看的信息，测试用
        // 获取共享参数保存的所有映射配对信息
        Map<String, Object> mapParam = (Map<String, Object>) shared.getAll();
        //获取共享的姓名数据
        if (mapParam.containsKey("name")) {
            userName = (String) mapParam.get("name");
        }
        //获取共享的密码数据
        if (mapParam.containsKey("pass")) {
            pass = (String) mapParam.get("pass");
        }

    }

    //跳转到注册页面
    public void abvfc(View view) {
        startActivity(new Intent(this, MainActivity2.class));
    }
}